package de.koller.planet2D.frame;

import com.sun.swing.internal.plaf.basic.resources.basic;
import de.koller.planet2D.PNode;
import de.koller.planet2D.container.RenderObj;
import de.koller.planet2D.frame.listener.ChangeListener;
import de.koller.planet2D.frame.uv.UVDesigner;
import de.koller.planet2D.rcom.AddPNode;
import de.koller.planet2D.rcom.RemovePNode;
import de.koller.planet2D.util.Parsers;

public class ObjectFrame extends PNode implements IPositionClickNotifier {
	
	public final ObjectFrameComp objectFrameComp = new ObjectFrameComp( this );
	private final boolean addObj;
	public final RenderObj renderObj;
	private final ChangeListener changeListener = new ChangeListener( this );

	private boolean textured = false;
	
	public ObjectFrame() {
		renderObj = new RenderObj();
		addObj = true;
		textured = true;
	}

	public ObjectFrame( RenderObj obj ) {
		renderObj = obj;
		objectFrameComp.posX.setText( obj.position.getX() + "" );
		objectFrameComp.posY.setText( obj.position.getY() + "" );
		objectFrameComp.sizeX.setText( obj.size.getX() + "" );
		objectFrameComp.sizeY.setText( obj.size.getY() + "" );
		objectFrameComp.rotation.setText( obj.rotation + "" );
		addObj = false;
	}
	
	@Override
	protected void init() {
		super.init();
		
		objectFrameComp.setVisible( true );
		proot.previewFrame.renderPanel.clickNotifier.add( this );
		
		if( addObj ) {
			proot.renderContainer.addChild( renderObj );
			if( textured ) renderObj.setTextured();
		}
		
		if( renderObj.getTextureEntity() == null ) 
			objectFrameComp.showPreviewTexture.setEnabled( false );
		else
			objectFrameComp.showPreviewTexture.setSelected( renderObj.getTextureEntity().textureInPreview );
		
		objectFrameComp.delButton.setEnabled( renderObj.deletable );
		objectFrameComp.proppane.setEnabledAt( 1, renderObj.getTextureEntity() != null );
		
		objectFrameComp.posX.getDocument().addDocumentListener( null );
		
		objectFrameComp.posX.getDocument().addDocumentListener( changeListener );
		objectFrameComp.posY.getDocument().addDocumentListener( changeListener );
		objectFrameComp.sizeX.getDocument().addDocumentListener( changeListener );
		objectFrameComp.sizeY.getDocument().addDocumentListener( changeListener );
		objectFrameComp.rotation.getDocument().addDocumentListener( changeListener );
		
		proot.previewFrame.setCurrent( this );
		
	}

	@Override
	protected void update( int elapsedTime ) {
		super.update( elapsedTime );
		
		renderObj.position.setX( Parsers.getValue( renderObj.position.getX(), objectFrameComp.posX.getText() ) );
		renderObj.position.setY( Parsers.getValue( renderObj.position.getY(), objectFrameComp.posY.getText() ) );
		renderObj.size.setX( Parsers.getValue( renderObj.size.getX(), objectFrameComp.sizeX.getText() ) );
		renderObj.size.setY( Parsers.getValue( renderObj.size.getY(), objectFrameComp.sizeY.getText() ) );
		renderObj.rotation = Parsers.getValue( renderObj.rotation, objectFrameComp.rotation.getText() );
	}

	@Override
	protected void render( int elapsedTime ) {
		super.render( elapsedTime );
		
		objectFrameComp.basicPrev.repaint();
	}

	
	@Override
	protected void release() {
		super.release();
		
		proot.previewFrame.renderPanel.clickNotifier.remove( this );
		objectFrameComp.dispose();
		
		proot.previewFrame.setCurrent( null );
	}

	@Override
	public void notifyPosition( double x, double y ) {
		objectFrameComp.posX.setText( x + "" );
		objectFrameComp.posY.setText( y + "" );
	}
	
	public void closeThis() {
		proot.call( new RemovePNode( this ) );
		proot.updateThis();
	}
	
	public void deleteObj() {
		proot.call( new RemovePNode( renderObj ) );
		closeThis();
	}

	public void editTextur( int i ) {
		if( i == 0 )
			proot.call( new AddPNode( this, new UVDesigner( renderObj.getTextureEntity().basicTexture ) ) );
	}
}
